package com.deyuanyun.pic.insp.service.statistics.impl;

import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.insp.mapper.DefectOtherMapper;
import com.deyuanyun.pic.insp.mapper.InspStatMapper;
import com.deyuanyun.pic.insp.service.inspresult.impl.BaseServiceImpl;
import com.deyuanyun.pic.insp.service.statistics.OtherDefectService;
import com.deyuanyun.pic.settings.support.biz.CodeUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.tags.Param;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 其他金属损失查询类
 *
 * @author       // scalpel.王敏
 * @version      // [v1.0] 创建时间（2016/10/14 9:23）
 * @see         // 引用的类
 * Method List:
 *         1.----------------findStatistics 对外暴露方法
 *         2.----------------structurePram 参数构造方法
 * History:                           // 历史修改记录           (如果有修改别人的类，必须)   [修改人][时间][修改描述]
 */

@Service
public class OtherDefectServiceImpl extends BaseServiceImpl implements OtherDefectService{
	
	@Resource
	private DefectOtherMapper otherMapper;

	@Resource
	InspStatMapper inspStatMapper;






	public Map<String,Object> findStatistics(HashMap<String,Object> pram)
	{

		handlerParam(pram);

		//返回参数
		Map<String,Object> result=new HashMap<String,Object>();


		structurePram(pram);
		String taskId= (String) pram.get("taskId");

		//封装除环焊缝外的查询条件
		StringBuffer whereSb=new StringBuffer(" and i.deleted=0 ");
		whereSb.append(" and i.INSP_TASK_ID='").append(taskId).append("'");
		String where=whereSb.toString();

		//删除内容
		whereSb.delete(0,whereSb.length());

		//封装环焊缝查询条件
		whereSb.append(" and i.deleted=0 ").append(" and i.INSP_TASK_ID='").append(taskId).append("'");
		String where2=whereSb.toString();

		/**
		 *其他缺陷-螺旋(直)焊缝异常里程分布
		 * 包含螺旋焊缝和直焊缝
		 */
		pram.put("tables",stringsToList(new String[][]{{"PIC_INSP_DEFECT_PIPE_WELD i ",where}}));
		result.put("otherzgList",otherMapper.queryBar(pram));


		/**
		 * 其他缺陷-焊缝异常里程分布
		 */
		pram.put("tables",stringsToList(new String[][]{{"PIC_INSP_DEFECT_GIRTH_WELD  i ",where2}}));
		result.put("otherhList",otherMapper.queryBar(pram));

		//其他统计列表
		result.put("otherList",otherMapper.findStatisticsOtherList(pram));
		//凹陷时钟图{"PIC_INSP_DEFECT_ELLIPTIC i",where},
		pram.put("tables",stringsToList(new String[][]{{"PIC_INSP_DEFECT_DEPRESSED i",where},{"PIC_INSP_DEFECT_GIRTH_WELD  i  ",where2}
														,{"PIC_INSP_DEFECT_PIPE_WELD i",where},{"PIC_INSP_DEFECT_CRACK i",where},{"PIC_INSP_DEFECT_OTHER i",where}}));
		result.put("otherClick",otherMapper.queryTime(pram));

		/**
		 * 凹陷里程分布
		 */
		pram.put("tables",stringsToList(new String[][]{{"PIC_INSP_DEFECT_DEPRESSED i ",where}}));
		result.put("depressedBar",otherMapper.queryBar(pram));

		//凹陷列表
		result.put("depressedList",otherMapper.findStatisticsList(pram));

		//凹陷时钟图
		result.put("depressedClick",otherMapper.queryTime(pram));

		/**
		 * 焊缝异常里程分布
		 */
		pram.put("tables",stringsToList(new String[][]{{"PIC_INSP_DEFECT_GIRTH_WELD  i  ",where2},{"PIC_INSP_DEFECT_PIPE_WELD i",where}}));
		result.put("girthWeldBar",otherMapper.queryBar(pram));

		//凹陷时钟图
		result.put("girthWeldClick",otherMapper.queryTime(pram));

		//焊缝列表
		result.put("girthWeldList",otherMapper.findStatisticsgirthWeldList(pram));

		return result;
	}


	/**
	 * 1.构造分成10等份查询的产生信
	 * 2.构造金属损失峰值深度（%wt） 参数
	 * @param param
	 */
	private void structurePram(Map<String, Object> param) {
		//从map中取出页面传递过来的里程参数信息
		Double beginMileage = (Double) param.get("beginMileage");
		Double endMileage = (Double) param.get("endMileage");

		//假定里程信息都是数字
		Double dbeginMileage = beginMileage;//Double.parseDouble(beginMileage);
		Double dendMileage = endMileage;//Double.parseDouble(endMileage);

		//每等份绝对里程的长度
		Double average = (dendMileage - dbeginMileage) / 10;

		//里程查询对象
		List<AssistQuery> queryList = new ArrayList<AssistQuery>();
		for (int i = 1; i <= 10; i++) {
			Double end = dbeginMileage + average;
			if (i == 10) {
				end = dendMileage;
			}
			queryList.add(new AssistQuery(dbeginMileage, end, "" + Math.floor(end)));
			dbeginMileage = end;
		}
		param.put("queryList", queryList);
	}

	/**
	 * 字符串数组转换为LIST
	 * @param p
	 * @return
     */
	public List<AssistTable> stringsToList(String[][] p)
	{
		List<AssistTable> data=new ArrayList<AssistTable>();
		for(int i=0;i<p.length;i++)
		{
			data.add(new AssistTable(p[i][0],p[i][1]));
		}
		return data;
	}

	/**
	 *  辅助查询表
	 *  表名
	 *  查询条件
	 */
	public class AssistTable
	{
		String table;
		String where;
		public AssistTable(String table,String where)
		{
			this.table=table;
			this.where=where;
		}

		public String getWhere() {
			return where;
		}

		public void setWhere(String where) {
			this.where = where;
		}

		public String getTable() {
			return table;
		}

		public void setTable(String table) {
			this.table = table;
		}
	}


	/**
	 * 辅助查询类
	 * 定义查询类型，
	 * 定义查询起止里程
	 * 勇于方便传递sql进行查询
	 */
	public class  AssistQuery
	{
		//区间信息
		Double begin=0d;
		Double end=0d;

		//查询展示名称
		String name="";

		//定义类构造函数
		public AssistQuery(Double begin,Double end,String name)
		{
			this.begin=begin;
			this.end=end;
			this.name=name;
		}

		public Double getBegin() {
			return begin;
		}

		public Double getEnd() {
			return end;
		}

		public String getName() {
			return name;
		}
	}

}
